﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Ultilities.Ultility;
    using ManagerClinic.Modules;
    using System.Linq;

    public partial class frmDmQuanHuyen : DevExpress.XtraEditors.XtraForm
    {
        GridCheckMarksSelection gridCheckMarksSelection = null;
        DmQuocGiaBusiness dmQuocGiaBusiness = new DmQuocGiaBusiness();
        IList<DmQuocGiaEntity> lstDmQuocGiaEntity = null;

        DmTinhThanhBusiness dmTinhThanhBusiness = new DmTinhThanhBusiness();
        IList<DmTinhThanhEntity> lstTinhThanhEntity = null;

        DmQuanHuyenBusiness dmQuanHuyenBusiness = new DmQuanHuyenBusiness();
        IList<DmQuanHuyenEntity> lstDmQuanHuyenEntity = null;

        public frmDmQuanHuyen()
        {
            InitializeComponent();

            gridCheckMarksSelection = new GridCheckMarksSelection(gvQuanHuyen);
            LoadQuocGia();
            LoadTinhThanh();
        }

        void LoadQuocGia()
        {
            lstDmQuocGiaEntity = dmQuocGiaBusiness.GetDmQuocGiaList();
            var dmQuocGiaEntity = new DmQuocGiaEntity()
            {
                MaQuocGia = ConstantManagerClinic.sValueMember,
                TenQuocGia = ConstantManagerClinic.sDisplayMember
            };
            lstDmQuocGiaEntity.Insert(0, dmQuocGiaEntity);

            lkQuocGia.Properties.DataSource = lstDmQuocGiaEntity;
            lkQuocGia.Properties.DisplayMember = "TenQuocGia";
            lkQuocGia.Properties.ValueMember = "MaQuocGia";
            lkQuocGia.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadTinhThanh()
        {
            lstTinhThanhEntity = dmTinhThanhBusiness.GetDmTinhThanhList();
            if (lstTinhThanhEntity.Count > 0 && lkQuocGia.EditValue.ToString() != ConstantManagerClinic.sValueMember)
            {
                lstTinhThanhEntity = lstTinhThanhEntity.Where(n => n.MaQuocGia == lkQuocGia.EditValue.ToString()).ToList();
            }

            var dmTinhThanhEntity = new DmTinhThanhEntity()
            {
                MaTinhThanh = ConstantManagerClinic.sValueMember,
                TenTinhThanh = ConstantManagerClinic.sDisplayMember
            };
            lstTinhThanhEntity.Insert(0, dmTinhThanhEntity);

            lkTinhThanh.Properties.DataSource = lstTinhThanhEntity;
            lkTinhThanh.Properties.DisplayMember = "TenTinhThanh";
            lkTinhThanh.Properties.ValueMember = "MaTinhThanh";
            lkTinhThanh.EditValue = ConstantManagerClinic.sValueMember;
        }

        private void lkQuocGia_EditValueChanged(object sender, EventArgs e)
        {
            LoadTinhThanh();
        }

        void LoadDataGrid(object sender, EventArgs e)
        {
            lstDmQuanHuyenEntity = dmQuanHuyenBusiness.GetDmQuanHuyenList();

            if (lkQuocGia.EditValue.ToString() != ConstantManagerClinic.sValueMember && lkTinhThanh.EditValue.ToString() != ConstantManagerClinic.sValueMember)
            {
                lstDmQuanHuyenEntity = lstDmQuanHuyenEntity.Where(n => n.MaTinhThanh == lkTinhThanh.EditValue.ToString()).ToList();
            }

            grdQuanHuyen.DataSource = lstDmQuanHuyenEntity;
        }

        private void lkTinhThanh_EditValueChanged(object sender, EventArgs e)
        {
            LoadDataGrid(sender, e);
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmQuanHuyen((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        DmQuanHuyenEntity getDataDmQuanHuyenEntity()
        {
            if (gvQuanHuyen.RowCount > 0)
            {
                return (DmQuanHuyenEntity)gvQuanHuyen.GetFocusedRow();
            }

            return null;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var dmQuanHuyenEntity = getDataDmQuanHuyenEntity();
            if (dmQuanHuyenEntity == null) return;

            var frm = new frmCapNhatDmQuanHuyen((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmQuanHuyenEntity);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }

            foreach (DmQuanHuyenEntity dm in deleteObjects)
            {
                dmQuanHuyenBusiness.DeleteDmQuanHuyen(dm.MaQuanHuyen);
            }

            gridCheckMarksSelection.ClearSelection();
            LoadDataGrid(sender, e);
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmDmQuanHuyen_Load(object sender, EventArgs e)
        {
            Authenticate.IsAuthenticateForm(this);
            LoadDataGrid(sender, e);
        }

        private void gvQuanHuyen_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        private void grdQuanHuyen_DoubleClick(object sender, EventArgs e)
        {
            if (btnSua.Enabled == false) return;
            btnSua_Click(sender, e);
        }
    }
}